package org.example;
//移除元素
public class topic27 {
    //暴力循环，将目标元素之后的元素往前移动，然后返回数组长度
    public int removeElement(int[] nums, int val) {
        int size=nums.length;
        for (int i = 0; i <nums.length;  i++) {
            if (nums[i] == val) {
                for (int j = i; j < nums.length - 1; j++) {
                    nums[j] = nums[j - 1];
                }
                i--;
                size--;
            }
        }
        return size;
    }
    //双指针，快慢指针
    public int removeElement2(int[] nums, int val) {
        int slow = 0;
        for (int fast = 0; fast < nums.length; fast++) {
            if (nums[fast] != val) {
                nums[slow] = nums[fast];
                slow++;
            }
        }
        return slow;
    }

}
